<?php
class Table{
	private $tableName;			//表名
	private $db;				//链接的数据库
	private $tablePre;			//表名前缀

	public function __construct($tableName){
		$this->tableName = $tableName;
		$this->db = DB::getInstance();
		$dbconfig = rareConfig::getAll('db');
		$this->tablePre = $dbconfig['default']['tablePre'];
	}
	/**
	*获取全部的数据
	*@where 查询条件
	*@reutrn 返回查询到的数据，返回格式为数组
	*/
	public function getAll($where){
		$sql = "select * from ".$this->tablePre.$this->tableName." where ".$where;
		$result = $this->db->select($sql);
		$return = $this->returnToArray($result);
		return $return;
	}
	/**
	  *获取数据列表
	  *@where 查询条件
	  *@limit 获取的条数
	  *@return 返回查询到的数据，返回格式为数组
	  */
	public function getList($where = '1',$limit='0'){
		$sql = "select * from ".$this->tablePre.$this->tableName." where ".$where;
		if($limit){
			$sql .= " limit {$limit}";
		}
		$result = $this->db->select($sql);
		$return = $this->returnToArray($result);
		return $return;
	}
	/**
	  *把select的返回结果转化为数组形式
	  *@result select语句返回的数据
	  *@return 转化后的数组
	  */
	private function returnToArray($result){
		$return = array();
		if(empty($result)) return $return;
		foreach($result as $row){
			$return[] = $row;
		}
		return $return;
	}
	/**
	  *获取一条数据
	  *@where	查询条件
	  *以数组的形式返回一条数据
	  */
	public function getOne($where = 1){
		$sql = "select * from ".$this->tablePre.$this->tableName." where ".$where;
		$result = $this->db->select($sql);
		$return = $this->returnToArray($result);
		if(empty($return)){
			return array();
		}else{
			return $return[0];
		}
	}
	/**
	  *根据条件删除数据
	  *@where 删除条件
	  *条件为空返回false，否则返回delete的执行结果
	  */
	public function del($where = ''){
		if($where == '') return false;
		$sql = "delete from ".$this->tablePre.$this->tableName." where ".$where;
		$result = $this->db->del($sql);
		return $result;
	}
	/**
	  *更新数据
	  *@where 条件
	  *@params 需要更新的数据，格式$params = array('id'=>1,'name'=>'milo')
	  *条件为空，返回false，否则返回update的执行结果
	  */
    
	public function update($where='',array $params){
		if($where == '') return false;
		$sql = "update ".$this->tablePre.$this->tableName." set ".$this->paramsToUpdateStr($params)." where ".$where;
		return  $this->db->update($sql);
		
	}
	/**
	  *把数组转化为以‘,’链接的字符串
	  */
	private function paramsToUpdateStr($params){
		if(empty($params)) return false;
		$str = '';
		foreach($params as $key=>$val){
			$str .= $key."= '".$this->beforIntoDB($val)."',";
		}
		return rtrim($str,',');
	}
	/**
	  *插入一条数据
	  *$params插入的数据，格式为$params = array('id'=>1,'name'=>'milo')
	  *$params为空，返回false,否则返回执行结果
	  *如果插入成功返回，插入行的ID
	  */
	public function insert($params){
		if(empty($params)) return false;
			$keys = $values = '';
		foreach($params as $key=>$val){
			$keys .= $key.',';
			$values .= "'".$this->beforIntoDB($val)."',";
		}
		$sql = "insert into ".$this->tablePre.$this->tableName." (".rtrim($keys,',').") values ";
		$sql .= "(".rtrim($values,',').")";
		if($this->db->insert($sql)){
			return $this->lastInsertID();
		}else{
			return 0;
		}
	}
	/**
	  *获取最后一次插入的ID号
	  */
	private function lastInsertID(){
		return $this->db->lastInsertID();
	}
	private function beforIntoDB($str){
		if(get_magic_quotes_gpc()){
			return addcslashes($str,';');
		}else{
			return addcslashes(addslashes($str),';');
		}
	}
	public function getListPage($where,$size=10){
		$page=isset($_GET['p'])?(int)$_GET['p']:1;
		$page=$page>0? $page:1;
		$sql = "select * from ".$this->tablePre.$this->tableName." where ".$where;
		$result = $this->db->listPage($sql,$size,$page);
		list($list,$total)=$result;
		$pageInfo=array();
		$pageInfo['page']=$page;
		$pageInfo['size']=$size;
		$pageInfo['total']=$total;
		return array($list,new Pager($pageInfo));
	}
}
?>
